﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.SessionState;
using System.Security;
using System.Security.Permissions;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ACM.cmsarea08.DBManager;
using ACM.cmsarea08;
using ACM.cmsarea08.mainconfiguration;
using System.Xml;


namespace ACM.cmsarea08.Administration.Controls 
{
	/// <summary>
	/// Description of WebControl1
	/// </summary>
	public class UserLogin : UserControl
	{	
		const string providerName = "Itan_MembershipProvider";
		//protected Login login_manager001;
		protected LinkButton chgPw;
		protected LinkButton logout;
		protected LinkButton setLang;
		protected DropDownList dropLingue;
		protected System.Resources.ResourceManager rm;
		
			
			
		protected ACM.cmsarea08.Administration.Core.DBClass dclass = new ACM.cmsarea08.Administration.Core.DBClass();
		override protected void OnInit(EventArgs e)
		{
			//HttpContext.Current.Response.Write("nome: " + Name);
			base.OnInit(e);
			if(Application["RM_ADM"]!=null)
    			rm = (System.Resources.ResourceManager) Application["RM_ADM"];
			InitializeComponent();
		
		}
		
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.OnLoad);
			this.logout.Click += new EventHandler(LogoutMe);
			this.setLang.Click += new EventHandler(Change);
		}
		
		
		private void OnLoad(object sender, EventArgs e)
		{
			chgPw.Text = rm.GetString("changepw");
			logout.Text = rm.GetString("logout");
			setLang.Text = rm.GetString("setlanguage");
			
			
			if(!Page.IsPostBack) {
				this.setLang.Attributes.Add("onclick","javascript:return confirm('If you change language at runtime you can lose any not saved data.');");
				RiempiDrop();
			}
		}
		private void RiempiDrop(){
			DataSet dsdrop = dclass.ExecuteDataset(string.Format("select cultureinfo,nome_lingua,predef " +
			                                                     "from {0}lingue order by predef desc; ",ACM.cmsarea08.mainconfiguration.MainConf.PrefissoTabelle));
			dropLingue.Items.Clear();
			if(dsdrop!=null){
				foreach(DataRow dr in dsdrop.Tables[0].Rows){
					dropLingue.Items.Add(new ListItem(dr["nome_lingua"].ToString(),dr["cultureinfo"].ToString()));
				}
			} 
			dropLingue.SelectedValue = ACM.cmsarea08.Users.CurrentUser.PrefLanguage;
		}
		
		
		
		
		private void Change(object o ,EventArgs e){
			if(dropLingue.SelectedIndex!=-1) {
				if( ACM.cmsarea08.Users.CurrentUser.PrefLanguage != dropLingue.SelectedValue)  {
					System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(dropLingue.SelectedValue);
 			    	System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(dropLingue.SelectedValue);
 			    	ACM.cmsarea08.Users.CurrentUser.PrefLanguage = dropLingue.SelectedValue;
					Response.Redirect("/administration/default.aspx");
				}
			}
		}
		private void LogoutMe(object o ,EventArgs e) {
			
			if(this.Page==null || this.Page.IsValid){
				HttpContext.Current.Response.Cookies["UserLang_" + ACM.cmsarea08.Users.CurrentUser.Guid].Expires = DateTime.Now;
				ACM.cmsarea08.Users.CurrentUser = null;
				System.Web.Security.FormsAuthentication.SignOut();
				HttpContext.Current.Session.Abandon();
				this.Page.Response.Redirect("/default.aspx", false);
				
						
			}
		}

	}
}
